// pages/cart/submit/index.js

import { hasDrink, wantDrink} from '../../../api/eapi'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    windowx:'0',
    windowWidth:null,
    movabel: false,
    title:'My Drink',
    auto:true,
    nav:true,
    options:false,
    hasDrink: [],
    wantDrink: [],
    selectAll: false
  },
  nav1(){
    let isSelectAll = true;
    this.data.wantDrink.forEach(item => {
      if(!item.select) {
        isSelectAll = false;
      }
    })
    this.setData({
      nav:true,
      selectAll: isSelectAll
    })
  },
  nav2(){
    let isSelectAll = true;
    this.data.hasDrink.forEach(item => {
      if(!item.select) {
        isSelectAll = false;
      }
    })
    this.setData({
      nav: false,
      movable:false,
      selectAll: isSelectAll
    })
  },
  movable(e){
     
  },
  manage(){
    this.setData({
      windowx:'0',
      options:!this.data.options,
      auto:!this.data.auto,
      movabel:!this.data.movabel
    })
  },
  select(e) {
    let target = e.currentTarget.dataset;
    let isWant = this.data.nav;
    let articles = isWant ? this.data.wantDrink : this.data.hasDrink;
    let isSelectAll = true;

    if(target.type === 'selectAll') {
      
      if(!this.data.selectAll) {
        articles.forEach(item => item.select = true);
        isWant ? this.setData({wantDrink: articles, selectAll: true}) :
        this.setData({hasDrink: articles, selectAll: true});
        
        return ;
      } else {
        articles.forEach(item => item.select = false);
        isWant ? this.setData({wantDrink: articles, selectAll: false}) :
        this.setData({hasDrink: articles, selectAll: false});
     
        return ;
      } 
    
    }
    

    articles.forEach(item => {
      if(item.product_id === target.id) {
        item.select = !item.select;
      }
    })

    articles.forEach(item => {
      if(!item.select) {
        isSelectAll = false;
      }
    })

    isWant ? this.setData({wantDrink: articles, selectAll: isSelectAll}) :
    this.setData({hasDrink: articles, selectAll: isSelectAll})
    
  },
  del(e) {

    let target = e.target.dataset;
    let ids = "";
    let isWant = this.data.nav; // 判断当前是想喝还是已喝 true为想喝

    if(target.type !== 'multi') {
      ids += target.id;
    }else {  
      // 获取所有被选择的id
      ids = isWant ? this.data.wantDrink.filter(item => item.select).map(item => item.product_id).join(',')
      : this.data.hasDrink.filter(item => item.select).map(item => item.product_id).join(',');
    }

    // 进行删除请求
    if(isWant) {
      if(ids === "") return ;
      wantDrink.delete({ids: ids}).then(resp => {
        console.log(resp)
        wx.showToast({title: '删除中', icon:'loading'})
        wantDrink.get().then(resp=>{
          console.log(resp)
          resp.data.forEach(function(item){
            item.select = false
          });
          this.setData({wantDrink: resp.data, selectAll: false},()=>wx.showToast({title: '完成'}))
        })
      })
    }else {
      if(ids === "") return ;
      hasDrink.delete({ids: ids}).then(resp => {
        console.log(resp)
        wx.showToast({title: '删除中', icon:'loading'})
        hasDrink.get().then(resp=>{
          console.log(resp)
          resp.data.forEach(function(item){
            item.select = false
          });
          this.setData({hasDrink: resp.data, selectAll: false},()=>wx.showToast({title: '完成'}))
        })
      })
    };
    this.setData({
      windowx:'0'
    })
    
    wx.setStorage({
      key: 'drink',
      data: true,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let wind = wx.getSystemInfoSync().windowWidth
    this.setData({
      windowWidth:wind
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let hasDrinkData, wantDrinkData;
    hasDrink.get().then(resp => {
      resp.data.forEach(function(item){
        item.select = false
      });
      hasDrinkData = resp.data;
      wantDrink.get().then(resp => {
        resp.data.forEach(function(item) {
          item.select = false
        });
        wantDrinkData = resp.data
        this.setData({hasDrink: hasDrinkData, wantDrink: wantDrinkData},() => {
          console.log(this.data)
        });
      }).catch(err => console.error(`[wantDrink]: ${err}`))
    }).catch(err => console.error(`[hasDrink]: ${err}`)) 
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  
})